package com.five.welcome.controller;


import com.five.welcome.entity.WelcomeAnnouncement;
import com.five.welcome.result.Result;
import com.five.welcome.service.WelcomeAnnouncementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * @author ywt
 * @since 2024-07-15
 */
@RestController
@CrossOrigin
@Tag(name = "迎新公告接口")
@RequestMapping("/welcomeAnnouncement")
public class WelcomeAnnouncementController {

    @Autowired
    private WelcomeAnnouncementService welcomeAnnouncementService;

    //查询所有公告
    @Operation(summary = "查询所有公告")
    @GetMapping("/getALlAnnounce")
    public Result getALlAnnounce() {
        List<WelcomeAnnouncement> aLlAnnounce = welcomeAnnouncementService.getALlAnnounce();
        return Result.ok(aLlAnnounce);
    }

    //查询显示在首页的公告
    @Operation(summary = "查询显示在首页的公告")
    @GetMapping("/getAnnounceOnStart")
    public Result getAnnounceOnStart() {
        WelcomeAnnouncement announcement = welcomeAnnouncementService.getAnnounceOnStart();
        return Result.ok(announcement);
    }

    //新增公告
    @Operation(summary = "新增公告")
    @PostMapping("/addAnnounce")
    public Result addAnnounce(@RequestBody WelcomeAnnouncement welcomeAnnouncement) {
        return welcomeAnnouncementService.
                addAnnounce(welcomeAnnouncement) > 0 ? Result.ok() : Result.error("添加失败!");
    }

    //删除公告
    @Operation(summary = "删除公告")
    @DeleteMapping("/delete/{id}")
    public Result deleteAnnounceById(@PathVariable Integer id) {
        return welcomeAnnouncementService.
                deleteAnnounceById(id) > 0 ? Result.ok() : Result.error("删除失败!");
    }

    //编辑公告
    @Operation(summary = "编辑公告审核")
    @PutMapping("/updateAnnounceById")
    public Result updateAnnounceById(@RequestBody WelcomeAnnouncement welcomeAnnouncement) {
        return welcomeAnnouncementService.
                updateAnnounceById(welcomeAnnouncement) > 0 ? Result.ok() : Result.error("修改失败!");
    }

    //修改公告状态
    @Operation(summary = "修改公告状态")
    @PutMapping("/updateAnnounceStatusById")
    public Result updateAnnounceStatusById(Integer announcementStatus,
                                           Integer announcementId) {
        String message = "";
        int ultimate = welcomeAnnouncementService.
                updateAnnounceStatusById(announcementStatus, announcementId);
        if (ultimate > 0) {
            if (announcementStatus == 1) {
                message = "已通过";
            } else if (announcementStatus == 2) {
                message = "已驳回";
            }
            return Result.ok(message);
        }
        return Result.error("系统错误!");
    }

    //修改显示在首页的公告
    @Operation(summary = "修改显示在首页的公告")
    @PutMapping("/modOnStartById")
    public Result modOnStartById(Integer id,Integer onStart){
        welcomeAnnouncementService.modOnStartById(id,onStart);
        return Result.ok();
    }
}

